//
//  MyWalletViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/3/28.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MyWalletViewController.h"
#import "UIView+Extension.h"
#import "MoneyViewController.h"//资金
#import "AccountViewController.h"//账户
#import "CouponsViewController.h"//优惠券
#import "WithdrawViewController.h"//提现
#import "BurseViewController.h"//充值
#import "SetPayWordViewController.h"//设置支付密码
#import "MyCouponViewController.h"
#import "ZJSecuritycodeViewController.h"

@interface MyWalletViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSArray     * arry;
@property (nonatomic, strong) NSDictionary     * dic;


@end

@implementation MyWalletViewController
-(NSDictionary*)dic{
    if(!_dic){
        _dic=[NSDictionary dictionary];
    }
    return _dic;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    //self.navigationController.navigationBar.hidden=YES;
    self.isShowDiag=NO;
    [self loadcountDetailRequest];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的钱包";
    [self createdUI];
    self.arry=[NSArray arrayWithObjects:MyString(@"提现"),MyString(@"充值"),MyString(@"设置支付密码"), nil];
}
- (void)createdUI
{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH+20) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    //[tableView registerNib:[UINib nibWithNibName:@"OrdersTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"OrdersCell"];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section==0) {
        return 100;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
//   判断
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        
        
        UIImageView * Hedview =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH*0.4-70)];
        Hedview.backgroundColor = Red_Color;
        //Hedview.image=[UIImage imageNamed:@"wallet_bg"];
//        UIImageView*freshView=[[UIImageView alloc]initWithFrame:CGRectMake(0, -200, MainW, 201)];
//        freshView.image=[UIImage imageNamed:@"placeholderImage"];
//        [Hedview addSubview:freshView];
        UIImageView *headImg=[[UIImageView alloc]init];
        UIButton *button=[[UIButton alloc]init];
        button.frame=CGRectMake(16, 32, 12, 18);
        [button setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backCilck) forControlEvents:UIControlEventTouchUpInside];
        //[self.view addSubview:button];
        [headImg sd_setImageWithURL:[NSURL URLWithString: [ZJUserDefaultsTools getAvatar]] placeholderImage:[UIImage imageNamed:@""]];
        headImg.center=Hedview.center;
        headImg.bounds=CGRectMake(0, 0, 60, 60);
        [Hedview addSubview:headImg];
        ViewRadius(headImg, 30);
        UILabel * nameNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, headImg.y+60, MainW, 30)];
        nameNumLabel.text = [NSString stringWithFormat:@"%@",[ZJUserDefaultsTools getCurrentNickName]];
        nameNumLabel.textColor=[UIColor whiteColor];
        nameNumLabel.textAlignment=1;
        nameNumLabel.font = FontAndStyle14;
        [Hedview addSubview:nameNumLabel];
        
        UIImageView*bottomView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MainW, 100)];
        bottomView.image=[UIImage imageNamed:@"wallet_bg"];
        bottomView.backgroundColor=[UIColor redColor];
        [Hedview addSubview:bottomView];
        NSArray *arr=[NSArray arrayWithObjects:MyString(@"银行卡"),MyString(@"资金"),MyString(@"优惠券"), nil];
        NSArray *imgArr=[NSArray arrayWithObjects:@"icon_card",@"icon_wallet",@"icon_coupon", nil];
        for (int i=0; i<3; i++) {
            UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake((MainW/6-15)+i*MainW/3, 25, 30, 25)];
            imag.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",imgArr[i]]];
            //imag.backgroundColor=[UIColor yellowColor];
            [bottomView addSubview:imag];
            UIView*v=[[UIView alloc]initWithFrame:CGRectMake(MainW/3+(MainW*i/3), 15, 1, 70)];
            v.backgroundColor=[UIColor whiteColor];
            [bottomView addSubview:v];
            MYLabel*label=[[MYLabel alloc]initWithFrame:CGRectMake((MainW*i/3), imag.bsg_bottom+5, MainW/3, 20)];
            label.textAlignment=1;
            label.textColor=[UIColor whiteColor];
            label.numberOfLines=0;
            //label.font=[UIFont systemFontOfSize:15];
            label.text=arr[i];
            if (i==1) {
                label.text=[NSString stringWithFormat:@"余额:\n%@元",self.dic[@"balance"]];
                label.frame=CGRectMake(MainW/3, imag.bsg_bottom+5, MainW/3, 40);
            }
            [bottomView addSubview:label];
            UIButton *button=[[UIButton alloc]init];
            //[button setTitle:@"点击没反应?" forState:0];
            button.frame =CGRectMake(0+i*MainW/3, bottomView.y, MainW/3, 70);
            button.tag=i;
            [button addTarget:self action:@selector(topbtnClick:) forControlEvents:UIControlEventTouchUpInside];
            //button.backgroundColor=[UIColor greenColor];
            [self.view addSubview:button];
            
        }
        return bottomView;

    }
    return nil;
}
#pragma mark -------资金点击事件
-(void)topbtnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
        {
            //NSLog(@"资金");
            MoneyViewController*vc=[[MoneyViewController alloc]init];
            vc.dic=[NSDictionary dictionary];
            vc.dic=self.dic;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 0:
        {
            //NSLog(@"账户");
            AccountViewController*vc=[[AccountViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            //NSLog(@"优惠券");
            [MBProgressHUD showSuccess:@"暂无优惠券!"];
//            MyCouponViewController*vc=[[MyCouponViewController alloc]init];
//            vc.str1=@"未使用(10)";
//            vc.str2=@"已使用(4)";
//            vc.str3=@"已过期(3)";
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark-------订单里的商品信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * hjID = [NSString stringWithFormat:@"%ldcell%ld",indexPath.section,indexPath.row];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell * hjcell = [tableView dequeueReusableCellWithIdentifier:hjID];
    if (!hjcell) {
        hjcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:hjID];
    }
    NSArray*arr=[NSArray arrayWithObjects:@"icon_deposit",@"icon_recharge",@"icon_password1", nil];
    hjcell.imageView.image=[UIImage imageNamed:arr[indexPath.section]];
    hjcell.imageView.frame=CGRectMake(5, 15, 40,40);
    hjcell.textLabel.text=[NSString stringWithFormat:@"%@",self.arry[indexPath.section]];
    hjcell.textLabel.font=FontAndStyle14;
    hjcell.textLabel.textColor=TextColor3;
    hjcell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    hjcell.selectionStyle=0;
    if (indexPath.section==0) {
        if ([self.dic[@"cashAmount"] intValue]!=0) {
            hjcell.detailTextLabel.text=[NSString stringWithFormat:@"提现中(¥ %@)",self.dic[@"cashAmount"]];
            hjcell.detailTextLabel.font=FontAndStyle14;
            hjcell.detailTextLabel.textColor=Red_Color;
        }
    }
    
    return hjcell;
}
//返回
-(void)backCilck{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            
            if ([self.dic[@"balance"] intValue]==0||[self.dic[@"hasPassword"] intValue]==0) {
                [self showToast:MyString(@"未设置")];
                return;
            }

            WithdrawViewController*vc=[[WithdrawViewController alloc]init];
            vc.dic=self.dic;
            [self.navigationController pushViewController:vc animated:YES];
            //NSLog(@"提现");
        }
            break;
        case 1:
        {
            BurseViewController*vc=[[BurseViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            //NSLog(@"充值");
        }
            break;
        case 2:
        {
            if ([self.dic[@"hasPassword"] intValue]==0) {
                ZJSecuritycodeViewController*vc=[[ZJSecuritycodeViewController alloc]init];
                vc.binkCart=2;
                vc.curphoneStr=[ZJUserDefaultsTools getCurrentUserPhone];
                [self.navigationController pushViewController:vc animated:YES];
                //NSLog(@"设置密码");
            }else{
                SetPayWordViewController*vc=[[SetPayWordViewController alloc]init];
                vc.statue=2;
                //vc.curphoneStr=[ZJUserDefaultsTools getCurrentUserPhone];
                [self.navigationController pushViewController:vc animated:YES];
                //NSLog(@"设置密码");
                
            }
        }
            break;
            
        default:
            break;
    }
}
-(void)loadcountDetailRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:ACCOUNTDETAIL method:GET dic:dic successBlock:^(NSDictionary *dic) {
        if ([dic[@"data"][@"hasPassword"] intValue]==1) {
            self.arry=[NSArray arrayWithObjects:MyString(@"提现"),MyString(@"充值"),MyString(@"修改支付密码"), nil];
        }
        self.dic=dic[@"data"];
        [self.tableView reloadData];
    }];
    
}
@end
